home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / wnos / wn941101 / nr4hdr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-10  |  3.4 KB  |  146 lines

  1. /* Net/rom transport layer header conversion routines.
  2.  * Copyright 1989 by Daniel M. Frank, W9NK.  Permission granted for
  3.  * non-commercial distribution only.
  4.  */
  5. #include "global.h"
  6. #include "config.h"
  7. #ifdef NETROM
  8. #include "mbuf.h"
  9. #include "nr4.h"
  10.  
  11. /* Convert a net/rom transport header to host format structure.
  12.  * Return -1 if error, 0 if OK.
  13.  */
  14. int
  15. ntohnr4(hdr,bpp)
  16. struct nr4hdr *hdr;
  17. struct mbuf **bpp;
  18. {
  19.     unsigned char tbuf[NR4MINHDR];
  20.     int i;
  21.  
  22.     if(pullup(bpp, (char *)tbuf, NR4MINHDR) < NR4MINHDR)
  23.         return -1;
  24.  
  25.     hdr->opcode = tbuf[4];
  26.  
  27.     switch(tbuf[4] & NR4OPCODE){
  28.         case NR4OPPID:        /* protocol ID extension */
  29.             hdr->u.pid.family = tbuf[0];
  30.             hdr->u.pid.proto = tbuf[1];
  31.             break;
  32.         case NR4OPCONRQ:    /* connect request */
  33.             hdr->u.conreq.myindex = tbuf[0];
  34.             hdr->u.conreq.myid = tbuf[1];
  35.             if((i = PULLCHAR(bpp)) == -1)
  36.                 return -1;
  37.             hdr->u.conreq.window = i;
  38.             if(pullup(bpp,hdr->u.conreq.user,AXALEN) < AXALEN)
  39.                 return -1;
  40.             if(pullup(bpp,hdr->u.conreq.node,AXALEN) < AXALEN)
  41.                 return -1;
  42.             break;
  43.         case NR4OPCONAK:    /* connect acknowledge */
  44.             hdr->yourindex = tbuf[0];
  45.             hdr->yourid = tbuf[1];
  46.             hdr->u.conack.myindex = tbuf[2];
  47.             hdr->u.conack.myid = tbuf[3];
  48.             if((i = PULLCHAR(bpp)) == -1)
  49.                 return -1;
  50.             hdr->u.conack.window = i;
  51.             break;
  52.         case NR4OPDISRQ:    /* disconnect request */
  53.             hdr->yourindex = tbuf[0];
  54.             hdr->yourid = tbuf[1];
  55.             break;
  56.         case NR4OPDISAK:    /* disconnect acknowledge */
  57.             hdr->yourindex = tbuf[0];
  58.             hdr->yourid = tbuf[1];
  59.             break;
  60.         case NR4OPINFO:        /* information frame */
  61.             hdr->yourindex = tbuf[0];
  62.             hdr->yourid = tbuf[1];
  63.             hdr->u.info.txseq = tbuf[2];
  64.             hdr->u.info.rxseq = tbuf[3];
  65.             break;
  66.         case NR4OPACK:        /* information acknowledge */
  67.             hdr->yourindex = tbuf[0];
  68.             hdr->yourid = tbuf[1];
  69.             hdr->u.ack.rxseq = tbuf[3];
  70.             break;
  71.         default:        /* what kind of frame is this? */
  72.             return -1;
  73.     }
  74.     return 0;
  75. }
  76.  
  77. /* Convert host-format level 4 header to network format */
  78. struct mbuf *
  79. htonnr4(hdr)
  80. struct nr4hdr *hdr;
  81. {
  82.     static int16 hlen[NR4NUMOPS] = {5,20,6,5,5,5,5};
  83.     struct mbuf *rbuf;
  84.     char *cp;
  85.     unsigned char opcode = hdr->opcode & NR4OPCODE;
  86.  
  87.     if(opcode >= NR4NUMOPS)
  88.         return NULLBUF;
  89.  
  90.     if(hdr == (struct nr4hdr *)NULL)
  91.         return NULLBUF;
  92.  
  93.     rbuf = ambufw(hlen[opcode]);
  94.  
  95.     rbuf->cnt = hlen[opcode];
  96.     cp = rbuf->data;
  97.  
  98.     cp[4] = hdr->opcode;
  99.  
  100.     switch(opcode){
  101.         case NR4OPPID:
  102.             *cp++ = hdr->u.pid.family;
  103.             *cp = hdr->u.pid.proto;
  104.             break;
  105.         case NR4OPCONRQ:
  106.             *cp++ = hdr->u.conreq.myindex;
  107.             *cp++ = hdr->u.conreq.myid;
  108.             cp += 3; /* skip to sixth byte */
  109.             *cp++ = hdr->u.conreq.window;
  110.             memcpy(cp,hdr->u.conreq.user,AXALEN);
  111.             cp += AXALEN;
  112.             memcpy(cp,hdr->u.conreq.node,AXALEN);
  113.             cp += AXALEN;
  114.             break;
  115.         case NR4OPCONAK:
  116.             *cp++ = hdr->yourindex;
  117.             *cp++ = hdr->yourid;
  118.             *cp++ = hdr->u.conack.myindex;
  119.             *cp++ = hdr->u.conack.myid;
  120.             cp++;    /* already loaded pid */
  121.             *cp = hdr->u.conack.window;
  122.             break;
  123.         case NR4OPDISRQ:
  124.             *cp++ = hdr->yourindex;
  125.             *cp = hdr->yourid;
  126.             break;
  127.         case NR4OPDISAK:
  128.             *cp++ = hdr->yourindex;
  129.             *cp = hdr->yourid;
  130.             break;
  131.         case NR4OPINFO:
  132.             *cp++ = hdr->yourindex;
  133.             *cp++ = hdr->yourid;
  134.             *cp++ = hdr->u.info.txseq;
  135.             *cp = hdr->u.info.rxseq;
  136.             break;
  137.         case NR4OPACK:
  138.             *cp++ = hdr->yourindex;
  139.             *cp++ = hdr->yourid;
  140.             *++cp = hdr->u.ack.rxseq;
  141.             break;
  142.     }
  143.     return rbuf;
  144. }
  145.  
  146. #endif /* NETROM */